(function (window, $, undefined) {
  'use strict';

  var menuData = [];

  var $roleList = $('#role-list');
  var roleData = {};

  main(function () {

    return util.tip.warn('该页面功能已经迁移至【角色管理】，请前往该页面进行权限管理的操作');

    // 菜单树结构
    util.get(ajaxURL.SYSTEM + '/sys/v1/menu/queryAll', function (res) {
      // 菜单数据转换
      menuData.length = 0;
      $.each(res.data, function (_, item) {
        menuData.push({
          kid: item.kid,
          pid: item.pid,
          name: item.name,
          levels: item.levels,
          _url: item.url
        });
      });

      getRoleList();
    });

    // 初始化树结构
    function initMenuTree(data) {
      var settings = {
        data: {
          simpleData: {
            enable: true,
            idKey: 'kid',
            pIdKey: 'pid',
            rootPId: null
          }
        },
        check: {
          enable: true
        },
        view: {
          showLine: true,
          showIcon: true,
          dblClickExpand: true,
          selectedMulti: false
        }
      }
      $.fn.zTree.init($('#menu-tree'), settings, data);
    }

    // 获取角色列表
    function getRoleList() {
      util.get(ajaxURL.SYSTEM + '/sys/v1/role/getList', function (res) {
        var ret = '';
        if (res.data && res.data.length > 0) {
          roleData = {};
          $.each(res.data, function (_, item) {
            roleData[item.kid] = item;
            ret += '<li title="' + item.name + '" data-kid="' + item.kid + '">';
            ret += item.name;
            ret += '</li>';
          });
          initMenuTree(menuData);
        } else {
          ret = '暂无角色，请先进行添加';
        }
        $roleList.html(ret);
      });
    }

    // 切换左侧的角色
    $roleList.on('click', 'li', function () {
      var $this = $(this);
      var id = $this.data('kid');
      var curData = roleData[id];

      if ($this.hasClass('on')) {
        return;
      }

      $this.addClass('on').siblings().removeClass('on');

      // 销毁树
      $.fn.zTree.destroy('menu-tree');

      getCurMenuList(id);
    });

    // 获取当前选择的角色的菜单列表
    function getCurMenuList(id) {
      util.get(ajaxURL.SYSTEM + '/sys/v1/role/get', {
        roleId: id
      }, function (res) {
        var curMenuData = [];
        var menuList = [];
        if (res.data && res.data.menuList) {
          $.each(res.data.menuList, function (_, item) {
            menuList.push(item.menuId);
          });
        }
        $.each(menuData, function (_, item) {
          curMenuData.push($.extend(true, item, {
            checked: menuList.indexOf(item.kid) !== -1
          }));
        });

        initMenuTree(curMenuData);
      });
    }

    $('#btn-save').click(function () {
      var $li = $roleList.find('li.on');
      if ($li.length === 0) {
        return util.tip.warn('请先选择角色');
      }
      var id = $li.data('kid');
      var treeObj = $.fn.zTree.getZTreeObj('menu-tree');
      var nodes = treeObj.getCheckedNodes(true);
      var curRoleData = roleData[id];
      var menuList = [];
      $.each(nodes, function (_, item) {
        menuList.push({
          createId: oUser.userId,
          roleId: id,
          menuId: item.kid
        });
      });

      var data = {
        createdId: oUser.userId,
        kid: id,
        name: curRoleData.name,
        description: curRoleData.description,
        menuList: menuList,
        updateId: oUser.userId
      };

      layer.confirm('确定设置当前角色：【' + curRoleData.name + '】的权限配置信息？', function () {
        util.post(ajaxURL.SYSTEM + '/sys/v1/roleMenu/update', menuList, function (res) {
          util.tip.success('设置成功');
        });
      });

    });

  });

})(window, jQuery);
